package com.bh.ly.oa.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bh.ly.oa.BaseActivity;
import com.bh.ly.oa.R;
import com.bh.ly.oa.adapter.OrderDetailPageAdater;
import com.bh.ly.oa.util.Config;
import com.bh.ly.oa.util.ListThread;

@SuppressLint("ShowToast")
public class OrderDetailContentlistViewActivity  extends BaseActivity{
	String data;
	OrderDetailPageAdater adater;
	List<String> list;
	ViewPager mViewPager;
	TextView titleTextView;
	private ImageButton backButton;
	private View orderView;
	
	public void setData(String data) {
		this.data = data;
	}
	
	String orderContentState = "1";
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.order_detail_pager_layout);
		this.mViewPager = (ViewPager) this.findViewById(R.id.view_pager);
		this.titleTextView = (TextView) this.findViewById(R.id.title_textview);
		this.backButton = ((ImageButton) this.findViewById(R.id.back_button));
		this.backButton.setImageResource(R.drawable.back);
		titleTextView.setText(R.string.group_title);
		backButton.setVisibility(View.VISIBLE);
		initListner();
		this.setData(getIntent().getStringExtra("json"));
//		setURLTYPE(Config.orderDetail);
//		this.setData(getIntent().getStringExtra("json"));
//		initDatas();
		
		list = new ArrayList<String>();
		list.add(orderContentState);
		list.add(data);
		
		adater= new OrderDetailPageAdater(this,list);
		adater.setArray(array);
		mViewPager.setAdapter(adater);
		
		start();
	}

	private void initListner() {
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int postion) {
				switch (postion) {
				case 0:
					titleTextView.setText(R.string.group_title);
					break;
				case 1:
					titleTextView.setText(R.string.chatlog_chat);
					break;
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		
	}
	
	protected void initDatas() {
		
	}
	
	protected List<JSONObject> array = new ArrayList<JSONObject>();
	protected ListThread mThread;
	protected Map<String, Object> map = new HashMap<String, Object>();
	
	protected void start() {
		try {
			this.map.put("order_number", new JSONObject(data).get("orderno").toString());
			mThread = new ListThread(handler, this.map, Config.orderDetail);
			mThread.start();
			list.set(0, "1");
			adater.notifyDataSetChanged();
		} catch (JSONException e) {
			e.printStackTrace();
			Toast.makeText(OrderDetailContentlistViewActivity.this, "加载数据出错了", Toast.LENGTH_SHORT).show();
		}
	}
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			JSONObject jsonObj = (JSONObject)msg.obj;
			JSONArray secontarray = null;
			try {
				secontarray = (JSONArray) jsonObj.getJSONArray("dataList");
			} catch (JSONException e1) {
				e1.printStackTrace();
			}

			if (secontarray == null) {
				list.set(0, "2");
			}else if(secontarray.length() == 0){
				list.set(0, "2");
			}else {
				array.clear();
				for (int i = 0; i < secontarray.length(); i++) {
					try {
						JSONObject ob = (JSONObject) secontarray.get(i);
						array.add(ob);
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				list.set(0, "0");
//				adater.setArray(array);
				mViewPager.setAdapter(adater);
			}
			
			adater.notifyDataSetChanged();
		};
	};
}
